﻿/*********************************************************************
 版权所有: 版权所有(C) 2010 ,熙软研发部
 模块编号:  ContactsBO
 作    者:范徐超
 编写日期:2010-3-6
 审    核:
 审核日期:
 描    述:ContactsBO 通讯录联系人信息  业务逻辑类
...........................................................................
如果模块有修改，则每次修改添加以下注释(以系统上线后为基准)：

 *********************************************************************/

using System;
using System.Data;
using System.Collections.Generic;
using LTP.Common;
using Cisoft.Cisoft3G.Model;
namespace Cisoft.Cisoft3G.BLL
{
    /// <summary>
    /// 业务逻辑类ContactsBO 的摘要说明。
    /// </summary>
    public class ContactsBO
    {
        private readonly Cisoft.Cisoft3G.DAL.ContactsDO dal = new Cisoft.Cisoft3G.DAL.ContactsDO();
        public ContactsBO()
        { }
        #region  成员方法

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return dal.GetMaxId();
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int CT_ContactID)
        {
            return dal.Exists(CT_ContactID);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Cisoft.Cisoft3G.Model.ContactsMO model)
        {
            dal.Add(model);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int AddWithResult(Cisoft.Cisoft3G.Model.ContactsMO model)
        {
            return dal.AddWithResult(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(Cisoft.Cisoft3G.Model.ContactsMO model)
        {
            dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int CT_ContactID)
        {

            dal.Delete(CT_ContactID);
        }
        
        /// <summary>
        /// 删除一条数据,Where
        /// </summary>
        public void Delete(string strWhere)
        {
            dal.Delete(strWhere);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Cisoft.Cisoft3G.Model.ContactsMO GetModel(int CT_ContactID)
        {

            return dal.GetModel(CT_ContactID);
        }

        public Cisoft.Cisoft3G.Model.ContactsMO GetModel(string strWhere)
        {
            return dal.GetModel(strWhere);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中。
        /// </summary>
        public Cisoft.Cisoft3G.Model.ContactsMO GetModelByCache(int CT_ContactID)
        {

            string CacheKey = "ContactsMOModel-" + CT_ContactID;
            object objModel = LTP.Common.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(CT_ContactID);
                    if (objModel != null)
                    {
                        int ModelCache = LTP.Common.ConfigHelper.GetConfigInt("ModelCache");
                        LTP.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (Cisoft.Cisoft3G.Model.ContactsMO)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Cisoft.Cisoft3G.Model.ContactsMO> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Cisoft.Cisoft3G.Model.ContactsMO> DataTableToList(DataTable dt)
        {
            List<Cisoft.Cisoft3G.Model.ContactsMO> modelList = new List<Cisoft.Cisoft3G.Model.ContactsMO>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Cisoft.Cisoft3G.Model.ContactsMO model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Cisoft.Cisoft3G.Model.ContactsMO();
                    if (dt.Rows[n]["CT_ContactID"].ToString() != "")
                    {
                        model.CT_ContactID = int.Parse(dt.Rows[n]["CT_ContactID"].ToString());
                    }
                    model.CT_Name = dt.Rows[n]["CT_Name"].ToString();
                    if (dt.Rows[n]["CT_Sex"].ToString() != "")
                    {
                        model.CT_Sex = int.Parse(dt.Rows[n]["CT_Sex"].ToString());
                    }
                    model.CT_Phone = dt.Rows[n]["CT_Phone"].ToString();
                    model.CT_Mobile = dt.Rows[n]["CT_Mobile"].ToString();
                    model.CT_Mobile2 = dt.Rows[n]["CT_Mobile2"].ToString();
                    model.CT_Email = dt.Rows[n]["CT_Email"].ToString();
                    model.CT_PostCode = dt.Rows[n]["CT_PostCode"].ToString();
                    model.CT_Address = dt.Rows[n]["CT_Address"].ToString();
                    model.CT_Company = dt.Rows[n]["CT_Company"].ToString();
                    model.CT_CompanyAddress = dt.Rows[n]["CT_CompanyAddress"].ToString();
                    model.CT_Department = dt.Rows[n]["CT_Department"].ToString();
                    model.CT_Duty = dt.Rows[n]["CT_Duty"].ToString();
                    model.CT_QQ = dt.Rows[n]["CT_QQ"].ToString();
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}

        #endregion  成员方法

        #region 根据手机号码返回姓名，若没有返回未识别 gwh
        /// <summary>
        /// 根据ID根据手机号码返回姓名，若没有返回未识别
        /// </summary>
        /// <param name="strContactsID">主键</param>
        /// <returns>姓名</returns>
        public string GetName(string strContactsID)
        {
            DataSet ds = new DataSet();
            ds = dal.GetName(" CT_Mobile='" + strContactsID+"'");

            if (ds.Tables[0].Rows.Count > 0)
            {
                return ds.Tables[0].Rows[0]["CT_Name"].ToString();
            }
            else
            {
                return "未识别";
            }
        }
        #endregion

        public DataSet GetListWithPage(int pageCurrent, int pageSize, string strWhere)
        {
            return dal.GetListWithPage(pageCurrent, pageSize, strWhere);
        }

        public int MobileIsExsist(string mobile)
        {
            return dal.MobileIsExsist(mobile);
        }
        public int MobileIsExsist(string mobileOld, string mobileNew)
        {
            if (mobileOld != mobileNew)
            {
                return dal.MobileIsExsist(mobileOld, mobileNew);
            }
            else
            {
                return 0;
            }
        }   

        public int GetListCount(int pageCurrent, int pageSize, string strWhere)
        {
            return dal.GetListCount(pageCurrent, pageSize, strWhere);
        }

        #region 三表查询,联系人和管理员关联
        public DataSet GetUserAndContact(string strWhere)
        {
            return dal.GetUserAndContact(strWhere);
        }

        /// <summary>
        /// 获得用户列表
        /// </summary>
        /// <param name="pageCurrent"></param>
        /// <param name="pageSize"></param>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public DataSet GetUserLst(int pageCurrent, int pageSize, string strWhere)
        {
            return dal.GetUserLst(pageCurrent,pageSize,strWhere);
        }

        /// <summary>
        /// 获得用户列表记录条数
        /// </summary>
        /// <param name="pageCurrent"></param>
        /// <param name="pageSize"></param>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public int GetUserLstCount(int pageCurrent, int pageSize, string strWhere)
        {
            return dal.GetUserLstCount(pageCurrent, pageSize, strWhere);
        }
        #endregion


    }
}

